<?php
namespace App\Library;
class Tree {
    public $arr;
    private $pkey;
    private $ckey;
    private $str;
    public $sign='┠';
    /**
     *  获取树结构
     *  $arr    array   数组
     *  $pkey   string  主键
     *  $ckey   sring   子级键
     */
    public function __construct($arr,$pkey,$ckey) {
        $arrs = array();
        foreach($arr as $v) {
            $arrs[$v[$pkey]] = $v;
        }
        $this->arr = $arrs;
        $this->pkey = $pkey;
        $this->ckey = $ckey;
		$this->get_arr();
    }
    public function get_arr() {//echo "<pre>"; print_r($this->arr);exit();
        $r = array();
        foreach ($this->arr as $id => $item) {
            if ($item[$this->ckey]) {
                $this->arr[$item[$this->ckey]]['child'][] = &$this->arr[$item[$this->pkey]];
                $r[] = $id;
            }
        }
        foreach($r as $u) {
            unset($this->arr[$u]);
        }
    }

	/*  输出
	$str = wr($tree->arr);
        global $str;
        $i = $l;
        foreach($arr as $k=>$v) {
            $n = str_repeat('&nbsp;&nbsp;&nbsp;',$l);
            if ($pid == 0) {
                $i=0;
            }
            if ($v['group_pid']==$pid) {
                $str .= '<option value="'.$v['group_id'].'">'.$n.$v['group_name'].'</option>';
                if ($v['child']) {
                    $this->wr_group($v['child'],$v['group_id'],$i+1);
                }
             }
        }
        return $str;*/
}

?>